// pages/cart/cart.js
import store from '../../store/index.js'
const app = getApp();
import {
  cartCountAdd,
  cartCountReduce,
  toggleChecked,
  cartCountDelete,
  toggleAllChecked
} from '../../store/actions/cart.js'
Page({
  /**
   * 读取state中的购物车数据
   */
  getCartData() {
    this.setData({
      cartList: store.getState().cart.cartList
    })
  },

  /**
   * 计算商品总价格
   */
  getTotalPrice() {
    let totalPrice = store.getState().cart.cartList.reduce((result, item) => {
      if(item.checked === true) {
        result += item.count*item.price
      }
      return result
    }, 0).toFixed(2)
    this.setData({
      totalPrice
    })
  },

  /**
   * 获取复选框状态(及选中状态商品种数)以改变全选框的状态
   */
  getCheckedStatus() {
    let cartListSize = store.getState().cart.cartList.length;
    let cartListCheckedSize = store.getState().cart.cartList.filter(item => {
      return item.checked === true
    }).length
    if (cartListSize === cartListCheckedSize) {
      this.setData({
        isAllChecked: true,
        cartListCheckedSize
      })
    } else {
      this.setData({
        isAllChecked: false,
        cartListCheckedSize
      })
    }
  },

  /**
   * 加商品数量
   */
  cartCountAdd(e) {
    let id = e.currentTarget.dataset.id
    store.dispatch(cartCountAdd(id))
    wx.setStorageSync('gy-cart', store.getState().cart.cartList)
  },

  /**
   * 减商品数量
   */
  cartCountReduce(e) {
    let id = e.currentTarget.dataset.id
    store.dispatch(cartCountReduce(id))
    wx.setStorageSync('gy-cart', store.getState().cart.cartList)
  },

  /**
   * 改变商品选中状态
   */
  cartItemCheck(e) {
    let id = e.currentTarget.dataset.id
    store.dispatch(toggleChecked(id))
    wx.setStorageSync('gy-cart', store.getState().cart.cartList)
  },

  /**
   * 删除商品
   */
  cartItemDelete(e) {
    let id = e.currentTarget.dataset.id
    store.dispatch(cartCountDelete(id))
    wx.setStorageSync('gy-cart', store.getState().cart.cartList)
  },

  /**
   * 切换全选状态
   */
  toggleAllChecked(e) {
    this.setData({
      isAllChecked: !this.data.isAllChecked
    })
    store.dispatch(toggleAllChecked(this.data.isAllChecked))
    wx.setStorageSync('gy-cart', store.getState().cart.cartList)
  },

  /**
   * 页面的初始数据
   */
  data: {
    cartList: null,
    isAllChecked: false,
    totalPrice: null,
    cartListCheckedSize:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCartData()
    // this.getTotalCount()
    this.getTotalPrice() 
    this.getCheckedStatus()
    store.subscribe(this.getCartData)
    // store.subscribe(this.getTotalCount)
    store.subscribe(this.getTotalPrice)
    store.subscribe(this.getCheckedStatus)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getTotalCount()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})